//
//  DZTabBarController.m
//  ShopApp
//
//  Created by 董筱泽 on 2017/8/6.
//  Copyright © 2017年 董筱泽. All rights reserved.
//

#import "DZTabBarController.h"
#import "UIColor+Addition.h"
#import "DZNavigationController.h"

@interface DZTabBarController ()

@end

@implementation DZTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 创建四个子控制器并设置它的相应内容
    UIViewController *vc1 = [self loadChildViewControllerWithClassName:@"DZHomeViewController" andTitle:@"首页" andImageName:@"v2_home"];
    
    
    UIViewController *vc2 = [self loadChildViewControllerWithClassName:@"DZCatalogueViewController" andTitle:@"分类" andImageName:@"v2_order"];
    
    UIViewController *vc4 = [self loadChildViewControllerWithClassName:@"DZCarViewController" andTitle:@"购物车" andImageName:@"shopCart"];
    
    
    UIViewController *vc5 = [self loadChildViewControllerWithClassName:@"DZMineViewController" andTitle:@"我的" andImageName:@"v2_my"];
    
    // 给标签控制器添加子控制器
    self.viewControllers = @[vc1, vc2, vc4, vc5];
    
    // 让tabBar没有半透明效果,还会影响它里面view的高度"默认view的最底部是贴着屏幕的最底部,如果氢此属性设置为NO,view的底部只到tabBar的顶部"
    self.tabBar.translucent = NO;
    
    // 设置标签栏的主题颜色"它会影响标签栏上的所有文字颜色"
    self.tabBar.tintColor = [UIColor colorWithHex:0x1e82d2];

}

- (UIViewController *)loadChildViewControllerWithClassName:(NSString *)className andTitle:(NSString *)title andImageName:(NSString *)imageName {
    
    
    // 把类名的字符串转成类的类型
    Class class =  NSClassFromString(className);
    // 通过转换出来的类的类型来创建控制器
    UIViewController *vc = [[class alloc] init];
    
    // 返回导航控制器"导航控制器中又添加了一个根控制器"
    return [self setupWithViewController:vc andTitle:title andImageName:imageName];
}

- (UIViewController *)setupWithViewController:(UIViewController *)vc andTitle:(NSString *)title andImageName:(NSString *)imageName {
    // 设置标签栏上对应控制器的标题
    vc.tabBarItem.title = title;
    //    vc.navigationItem.title
    // 设置控制器的标题会影响它的导航条或标签栏上的标题
    //    vc.title = title;
    // 设置默认状态的图片
    vc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 拼接选中状态的图片
    NSString *selectedImageName = [imageName stringByAppendingString:@"_r"];
    // 设置选中图片
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 创建导航控制器
    DZNavigationController *nav = [[DZNavigationController alloc] initWithRootViewController:vc];
    
    return nav;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
